package com.vince.model.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import com.vince.model.domain.User;
import com.vince.util.DBUtil;

/**
 *   
 *   Model层------service，业务层
 *     user表操作
 *   
 *    
 *    
 * @author susp
 *  
 */
public class UserService {
	//注册用户
	public void addUser(User user){
		   String sql="insert into user(user_name,password, create_date,last_modify_date,"
		       		+ " user_type) values(?,?,now(),now(),?)";
		   String[] param={user.getUsername(),user.getPassword(),user.getUsertype().toString()};
		   DBUtil.execute(sql,param);
	}
	
	//修改用户
	public void updateUser(){
		
	}
	
	//验证用户
	public boolean checkUser(User user) throws SQLException{
		//登录验证功能，可以封装起来
		System.out.println("checkuser.......");
		System.out.println("checkUser"+user.getUsername()+","+user.getPassword());
		boolean b=false;
		String sql="select * from user where user_name=? and password=?";
		//调用工具类完成数据库操作，DAO
		String[] param={user.getUsername(),user.getPassword()};
		ResultSet rs=DBUtil.exeQuery(sql, param);
	    if(rs.next()){
	    	b=true;
	    }
		return b;
	}

	
	//返回所有用户信息
	public static Collection getData(){
		String sql="select * from user where user_type=1";
		ResultSet rs=DBUtil.exeQuery(sql, null);
		Collection userlist=new ArrayList();
		try {
			while(rs.next()){
				User user=new User();
				user.setUsername(rs.getString(2));
				user.setUserid(rs.getInt(1));
				user.setUsertype(rs.getInt(6));
				userlist.add(user);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return userlist;

	}
}
